<script>

  // redux 核心
  function compose(...funcs) {
    return funcs.reduce((a, b) => (...args) => a(b(...args)))
  }

  // koa核心，调用next参数表示调用下一个函数
  function fn1(next) {
    console.log(1);
    next();
  }
  function fn2(next) {
    console.log(2);
    next();
  }
  function fn3(next) {
    console.log(3);
    next();
  }
  function compose(middleware) {
    function dispatch(index) {
      if (index == middleware.length) return;
      var curr;
      curr = middleware[index];
      // 这里使用箭头函数，让函数延迟执行
      return curr(() => dispatch(++index))
    }
    dispatch(0)
  };
  const middleware = [fn1, fn2, fn3];
  compose(middleware);

</script>

